package com.company.fastsbapi.service.impl;

import com.company.fastsbapi.dataobject.model.student;
import com.company.fastsbapi.dataobject.ro.studentAddRO;
import com.company.fastsbapi.dataobject.ro.studentUpdateRO;
import com.company.fastsbapi.repository.studentRepository;
import com.company.fastsbapi.service.studentService;
import com.company.fastsbapi.enums.ResultEnum;
import com.company.fastsbapi.exception.ServiceException;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
* Created by CodeGenerator on 2020/09/08.
*/
@Service
@Transactional
@Slf4j
public class studentServiceImpl implements studentService {

    @Autowired
    private studentRepository studentRepository;

    @Override
    public void addstudent(studentAddRO studentAddRO) {
        student student = new student();
        BeanUtils.copyProperties(studentAddRO, student);
        studentRepository.save(student);
    }

    @Override
    public void deletestudent(Integer id) {
        student student = studentRepository.getOne(id);
        if (student == null) {
            log.error("【deletestudent出错】id不存在,id:{}", id);
            throw new ServiceException(ResultEnum.ERROR_ID);
        }
        studentRepository.delete(id);

    }

    @Override
    public void updatestudent(studentUpdateRO studentUpdateRO) {
        student student = studentRepository.findOne(studentUpdateRO.getId());
        if (student == null) {
            log.error("【updatestudent出错】id不存在,id:{}", studentUpdateRO.getId());
            throw new ServiceException(ResultEnum.ERROR_ID);
        }
        BeanUtils.copyProperties(studentUpdateRO, student);
        studentRepository.save(student);
    }

    @Override
    public student findstudent(Integer id) {
        student student = studentRepository.findOne(id);
        if (student == null) {
            log.error("【findstudent出错】id不存在,id:{}", id);
            throw new ServiceException(ResultEnum.ERROR_ID);
        }
        return student;
    }

    @Override
    public Page<student> findstudentPage(PageRequest pageRequest) {
        return studentRepository.findAll(pageRequest);
    }

}